package com.flaskpy.web.eternity.core.filter;

import cn.hutool.core.util.StrUtil;
import com.flaskpy.web.eternity.core.constant.CommonConstant;
import com.flaskpy.web.eternity.core.holder.RequestNoHolder;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 生成响应id 方便以后日志查找
 */
public class RequestNoFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            String requestNo = StrUtil.uuid();
            HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
            httpServletResponse.addHeader(CommonConstant.REQUEST_NO_HEADER_NAME, requestNo);
            RequestNoHolder.set(requestNo);
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            RequestNoHolder.remove();
        }
    }
}
